On Error Resume Next
 
Dim installPath 
Dim addInName 
Dim addInFileName 
Dim objExcel 
Dim objAddin
 
'设定AddIns情报
addInName = "myEtExtends Addin" 
addInFileName = "myEtExtends.xlam"
 
IF MsgBox("Do you want to install " &addInName & " ?", vbYesNo + vbQuestion, addInName) = vbNo Then 
  WScript.Quit 
End IF
 
'Set objWshShell = CreateObject("WScript.Shell") 
'Set objFileSys = CreateObject("Scripting.FileSystemObject")
' 创建 WshShell 和 FileSystemObject 对象
Set objWshShell = CreateObject("WScript.Shell")
If Err.Number <> 0 Then
    WScript.Echo "Error creating WshShell object: " & Err.Description
    WScript.Quit
End If

Set objFileSys = CreateObject("Scripting.FileSystemObject")
If Err.Number <> 0 Then
    WScript.Echo "Error creating FileSystemObject: " & Err.Description
    WScript.Quit
End If


On Error GoTo 0 ' 恢复正常的错误处理


' 校验路径是否存在
basePath = objWshShell.SpecialFolders("AppData") & "\Microsoft\AddIns"

' 检查路径是否存在，如果不存在则创建它
If Not objFileSys.FolderExists(basePath) Then
    'WScript.Echo "Folder does not exist, attempting to create it."
    On Error Resume Next ' 忽略后续可能出现的错误
    objFileSys.CreateFolder(basePath)
    If Err.Number <> 0 Then
        WScript.Echo "Error creating folder: " & Err.Description
        WScript.Quit ' 终止脚本执行
    End If
    On Error GoTo 0 ' 恢复正常的错误处理
Else
    'WScript.Echo "Folder already exists."
End If

' 构建完整的安装路径
installPath = basePath & "\" & addInFileName

 
'创建安装路径
'(ex)C:\Users\[User]\AppData\Roaming\Microsoft\AddIns\[addInFileName] 
'installPath = objWshShell.SpecialFolders("Appdata") & "\Microsoft\Addins\" & addInFileName
 
'文件复制（覆盖）
objFileSys.CopyFile  addInFileName ,installPath , True
 
Set objFileSys = Nothing
 
'Excel 实例化
Set objExcel = CreateObject("Excel.Application") 
objExcel.Workbooks.Add
 
'AddIns注册
Set objAddin = objExcel.AddIns.Add(installPath, True) 
objAddin.Installed = True
 
'Excel 退出
objExcel.Quit
Set objAddin = Nothing 
Set objExcel = Nothing
 
IF Err.Number = 0 THEN 
   MsgBox "The add-ins have been installed.", vbInformation, addInName 
   'objWshShell.Run "xxxxxxxx_readme.txt"
ELSE 
   MsgBox "An error has occurred." & vbCrLF & "If Excel is running, exit it.", vbExclamation, addInName 
End IF
Set objWshShell = Nothing